package cn.lxh.ymcc.web.controller;

import cn.lxh.ymcc.dto.RegisterDto;
import cn.lxh.ymcc.service.IUserService;
import cn.lxh.ymcc.domain.User;
import cn.lxh.ymcc.query.UserQuery;
import cn.lxh.ymcc.result.JSONResult;
import cn.lxh.ymcc.result.PageList;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    @RequestMapping(value="/register",method= RequestMethod.POST)
    public JSONResult register(@RequestBody RegisterDto dto){
        userService.register(dto);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody User user){
        if(user.getId()!=null){
            userService.updateById(user);
        }else{
            userService.insert(user);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        userService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(userService.selectById(id));
    }
    /**
     * 根据电话查询
     */
    @RequestMapping(value ="/user/{phone}",method = RequestMethod.GET)
    public User selectByPhone(@PathVariable("phone") String phone){
        return userService.selectByPhone(phone);
    }
    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(userService.selectList(null));
    }
    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody UserQuery query){
        Page<User> page = new Page<User>(query.getPage(),query.getRows());
        page = userService.selectPage(page);
        return JSONResult.success(new PageList<User>(page.getTotal(),page.getRecords()));
    }
    /**
     * backpagelist--明天做
     * 黑名单 1的才能显示拉黑
     */
    @GetMapping("/backpagelist")
    public JSONResult backpageByState(Long bitState){
        return JSONResult.success(userService.backpageByState(bitState));
    }

    /**
     * 修改黑名单状态
     * @param id
     * @return
     */
    @RequestMapping(value="/backuser/{id}",method=RequestMethod.POST)
    public JSONResult backuser(@PathVariable("id") Long id){
        userService.backuser(id);
        return JSONResult.success();
    }
    /**
     * whitepagelist
     * 白名单
     */
    @GetMapping("/whitepagelist")
    public JSONResult whitepagelist(Long bitState){
        return JSONResult.success(userService.whitepagelist(bitState));
    }
    /**
     * 拉白 用户
     */
    @RequestMapping(value="/whiteuser/{id}",method=RequestMethod.POST)
    public JSONResult whiteuser(@PathVariable("id") Long id){
        userService.whiteuser(id);

        return JSONResult.success();
    }
}
